package com.twenty.seven.security.sign.service.impl;

import com.twenty.seven.security.sign.service.SignService;
import com.twenty.seven.security.sign.util.Constant;
import com.twenty.seven.toolbox.encrypt.rsa.RSAUtils;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * @Author 001
 * @Date 2020/1/16 11:21
 * @TODO
 **/
@Service
public class SignServiceImpl implements SignService {
    /**
     * 根据自己业务保存数据
     * @return
     * @throws Exception
     */
    @Override
    public Map<String, String> getSignAndAssessKey() throws Exception{

            Map<String,Object> map = RSAUtils.genKeyPair();
            //私钥
            String privateKey = RSAUtils.getPrivateKey(map);
            //公钥
            String publicKey = RSAUtils.getPublicKey(map);
            //签名
            String sign = RSAUtils.sign(Constant.SIGN_DATA,privateKey);
            Map<String,String> signMap = new HashMap<>();
            signMap.put("accessKey",publicKey);
            signMap.put("sign",sign);
            return signMap;
    }

    @Override
    public boolean verify(String sign, String assessKey) throws Exception {
        return RSAUtils.verify(Constant.SIGN_DATA,assessKey,sign);
    }
}
